﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Xpom_Xpum__Integrated_Development_Environment.Model.Controls;

namespace Xpom_Xpum__Integrated_Development_Environment.View.Controls
{
    /// <summary>
    /// Interaction logic for TabControlView.xaml
    /// </summary>
    public partial class TabControlView : UserControl
    {
        #region Constructor

        public TabControlView()
        {
            InitializeComponent();
            if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
            {
            }
            else
            {
                this.DataContext = ((App)App.Current).tcvwm;
            }
        }

        #endregion

        #region Commands

        #endregion

        #region Fields

        #endregion

        #region Properties

        #endregion

        #region Methods

        private void TabItem_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            var tabItem = e.Source as TabItem;

            if (tabItem == null)
                return;

            var poz = e.GetPosition(sender as TabItem);

            if (Mouse.PrimaryDevice.LeftButton == MouseButtonState.Pressed && poz.X < 10)
            {
                DragDrop.DoDragDrop(tabItem, tabItem, DragDropEffects.All);
            }
        }

        private void TabItem_Drop(object sender, DragEventArgs e)
        {
            var tabItemTarget = e.Source as TabItem;

            var tabItemSource = e.Data.GetData(typeof(TabItem)) as TabItem;


            if (!tabItemTarget.Equals(tabItemSource))
            {
                var tabControl = tabControl1;
                int sourceIndex = ((App)App.Current).tcvwm.ta.IndexOf(tabItemSource.Content as TabItemModel);
                int targetIndex = ((App)App.Current).tcvwm.ta.IndexOf(tabItemTarget.Content as TabItemModel);

                ((App)App.Current).tcvwm.ta.Move(sourceIndex, targetIndex);

                //ta.Remove(tabItemTarget.Content as TabIt);
                //ta.Insert(sourceIndex, tabItemTarget.Content as TabIt);
            }
        }

        private void TextEditor_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var currentTim = tabControl1.SelectedItem as TabItemModel;
            ((App)App.Current).tcvwm.Close(currentTim);
        }

        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var currentTim = tabControl1.SelectedItem as TabItemModel;
            //FocusManager.SetFocusedElement(currentTim.TextEditor.Parent, currentTim.TextEditor);
        }

        #endregion

    }
}
